/**上传单个图片和视频通用方法 */
module.exports = Behavior({
  methods: {
    /**上传图片 */
    img_upload(e) {
      let that = this;
      /**上传图片的对象 */
      let img = e.currentTarget.dataset.img;
      wx.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success(imgRes) {
          // tempFilePath可以作为img标签的src属性显示图片
          const tempFilePaths = imgRes.tempFilePaths;
          console.log("tempFilePaths", tempFilePaths);
          let photo = {}; //用于更新视图诗句
          let id_photo_front = photo[img] = that.data[img];
          id_photo_front.url = tempFilePaths[0];
          id_photo_front.state = "loading";
          console.log("photo", photo)
          that.setData(photo)
          wx.uploadFile({
            filePath: tempFilePaths[0],
            name: 'image',
            url: that.data.host + '/home/upload/image',
            formData: {
              from: 'verify'
            },
            success(res) {
              id_photo_front.state = "error";
              // id_photo_front.photo='';
              // console.log("home/upload/image", res);
              if (res.statusCode != 200) {
                return wx.showModal({
                  showCancel: false,
                  content: res.errMsg || '上传失败',
                })
              }
              let resObj = JSON.parse(res.data);
              if (resObj.code) {
                return wx.showToast({
                  title: resObj.msg,
                  icon: 'none'
                })
              }
              id_photo_front.state = "ok";
              id_photo_front.photo = resObj.data.path;

            },
            fail() {
              id_photo_front.state = "error";
              wx.showModal({
                showCancel: false,
                content: '上传接口异常',
              })
            },
            complete() {
              that.setData(photo)
            }
          });
        },
        fail(e) {
          wx.showToast({
            title: e.errMsg || '调用微信文件失败',
            icon: 'none'
          })
          // console.log(e);
        }
      })
    },
  }
})